<?php
/**
*	登录
*/
class loginClassAction extends apiAction
{
	public $logincheck = false;
	public function initsetAction()
	{
		$title = TITLE;
		$callback = $this->get('callback');
		$barr['title'] 	 = $title;
		$barr['reimurl'] = '?d=reim'; //客户端路径
		$result = json_encode($barr);
		if($callback){
			return ''.$callback.'('.$result.')';
		}else{
			return $result;
		}
	}
	
	public function exitAction()
	{
		m('login')->exitlogin();
		return returnsuccess();
	}
	
	/**
	*	读取可上传最大M
	*/
	public function getmaxupAction()
	{
		$maxup = c('upfile')->getmaxzhao();
		return returnsuccess(array(
			'maxup'    => $maxup,
			'apptheme' => getconfig('apptheme'),
			'regbool'	=> getconfig('regbool','0')
		));
	}
	
	public function logincheckAction()
	{
		$user = $this->post('user');
		$pass = $this->post('pass');
		$device = $this->post('device');
		$cform  = $this->post('cform','PC');
		
		return m('login')->start($user,$pass,$cform, $device);
	}
	
	/**
	*	注册
	*/
	public function regcheckAction()
	{
		$device = $this->post('device');
		$user = trim($this->post('user'));
		$pass = trim($this->post('pass'));
		$name = trim($this->post('name'));
		$mobile 	= trim($this->post('mobile'));
		$mobilecode = trim($this->post('mobilecode'));
		$code 		= trim($this->post('smscode'));
		$db   = m('users');
		
		if(!$device || !$pass || !$name)return returnerror('error');
		
		if(isempt($mobile))return returnerror('手机号不能为空');
		
		$to = $db->rows("`mobile`='$mobile'");
		if($to>0)return returnerror('手机号已注册过了');
		
		if(!c('sms')->checkcode($mobile, $code, 'reg', $device))return returnerror('手机短信验证码错误');
		
		$rand = 'reg'.rand(100000,999999);
		
		$uarr = array(
	
			'name' => $name,
			'mobile' 	=> $mobile,
			'mobilecode' => $mobilecode,
			'pass' => md5($pass.$rand),
			'regdt'   => $this->now,
			'rand' 	  => $rand,
		);
		$bo = $db->insert($uarr);
		if(!$bo)return returnerror(lang('reg.regerr'));
		return returnsuccess();
	}
	
	public function findcheckAction()
	{
		$device = $this->post('device');
		$pass = trim($this->post('pass'));
		$mobile 	= trim($this->post('mobile'));
		$mobilecode = trim($this->post('mobilecode'));
		$code 		= trim($this->post('smscode'));
		$db   = m('users');
		
		if(!$device || !$pass)return returnerror('error');
		if(isempt($mobile))return returnerror('手机号不能为空');
		
		$urs = $db->getone("`mobile`='$mobile'");
		if(!$urs)return returnerror('手机号没有注册');
		
		if(!c('sms')->checkcode($mobile, $code, 'find', $device))return returnerror('手机短信验证码错误');

		
		$rand = 'find'.rand(100000,999999);
		$pass = md5($pass.$rand);
		$db->update(array(
			'rand' => $rand,
			'pass' => $pass,
		), $urs['id']);

		return returnsuccess();
	}
	
	/**
	*	图片验证码
	*/
	public function imgcodeAction()
	{
		c('imgcode')->show($this->get('device'));
	}
	
	public function getcodeAction()
	{
		$device = $this->post('device');
		$gtype = $this->post('gtype');
		$mobilecode = $this->post('mobilecode');
		$mobile 	= $this->post('mobile');
		$imgcode 	= $this->post('imgcode');
		$gtypea 	= array('find','reg','edit','san');
		if(isempt($mobile))return returnerror('手机号不能为空');
		if(isempt($imgcode))return returnerror('计算验证结果不能为空');
		if(!in_array($gtype, $gtypea))return returnerror('无效类型');
		if(!c('imgcode')->check($device,$imgcode))return returnerror('计算验证结果错误',202);
		
		$to = m('users')->rows("`mobile`='$mobile'");
		if($gtype=='reg' || $gtype=='edit'){
			if($to>0 && $gtype=='reg')return returnerror('手机号已注册过了');
			if($to>0 && $gtype=='edit')return returnerror('手机号已使用过了');
		}else{
			if($to==0)return returnerror('此手机号未注册');
		}
		
		$barr = c('sms')->getcode($mobile,$gtype, $device);
		if(!$barr['success'])return $barr;
		
		$sj = substr($mobile,0,3).'****'.substr($mobile,-4);
		$msg	= '已成功发送到手机号：'.$mobilecode.''.$sj.'';
		
		return returnsuccess(array(
			'msg' => $msg,
			'time'=> time()
		));
	}
}